package handler

import (
	v1 "gin-vben-admin/api/v1"
	"gin-vben-admin/api/v1/errcode"
	"gin-vben-admin/internal/service"
	"github.com/gin-gonic/gin"
	"strconv"
)

type SysRoleHandler struct {
	*Handler
	sysRoleService service.SysRoleService
}

func NewSysRoleHandler(handler *Handler, sysRoleService service.SysRoleService) *SysRoleHandler {
	return &SysRoleHandler{
		Handler:        handler,
		sysRoleService: sysRoleService,
	}
}

func (h *SysRoleHandler) GetPageList(ctx *gin.Context) {
	var req v1.SysRoleListReq
	if err := ctx.ShouldBind(&req); err != nil {
		v1.Error(ctx, err)
		return
	}
	total, data, err := h.sysRoleService.GetPageList(ctx, &req)
	v1.PageData(ctx, total, data, err)
}

func (h *SysRoleHandler) GetOptions(ctx *gin.Context) {
	var req v1.SysRoleListReq
	if err := ctx.ShouldBind(&req); err != nil {
		v1.Error(ctx, err)
		return
	}
	total, data, err := h.sysRoleService.GetPageList(ctx, &req)
	list := make([]v1.DataOption, len(data))
	if err == nil && total > 0 {
		for i := range data {
			list[i] = v1.DataOption{
				Text:   data[i].RoleName,
				Value:  data[i].RoleId,
				Status: data[i].Status,
			}
		}
	}
	v1.Response(ctx, err, v1.DataOptions{Total: total, Options: list})
}

//func (h *SysMenuHandler) Detail(ctx *gin.Context) {
//	_id := ctx.Param("id")
//	id, err := strconv.ParseInt(_id, 10, 64)
//	if err != nil {
//		v1.Error(ctx, errcode.ErrBadRequest)
//		return
//	}
//	data, err := h.sysMenuService.Detail(ctx, id)
//	v1.Response(ctx, err, data)
//}

func (h *SysRoleHandler) Create(ctx *gin.Context) {
	var req = v1.SysRoleCreateReq{AuthId: UserId(ctx)}
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.Error(ctx, err)
		return
	}
	v1.Response(ctx, h.sysRoleService.Create(ctx, &req), nil)
}

func (h *SysRoleHandler) Update(ctx *gin.Context) {
	var req = v1.SysRoleUpdateReq{AuthId: UserId(ctx)}
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.Error(ctx, err)
		return
	}
	v1.Response(ctx, h.sysRoleService.Update(ctx, &req), nil)
}

func (h *SysRoleHandler) Delete(ctx *gin.Context) {
	_id := ctx.Param("id")
	id, err := strconv.ParseInt(_id, 10, 64)
	if err != nil {
		v1.Error(ctx, errcode.ErrBadRequest)
		return
	}
	v1.Response(ctx, h.sysRoleService.Delete(ctx, id), nil)
}
